package package1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

public class AutoSign {
	public static void main(String[] args) throws IOException, InterruptedException {
		String cmd = getCMD();
		File f = getFile();
		writeIntoFile(f,cmd);
		
		Thread.sleep(1000);
		Process p = run(f.getAbsolutePath());
		logToFile(p);
		
		System.err.println("over");
	}
	private static File getFile() {
		return new File(getDesktop()+"autoSign.bat");
	}
	private static void logToFile(Process p) throws IOException {
		BufferedWriter bw;
		BufferedReader infoBR = new BufferedReader(new InputStreamReader(p.getInputStream()));
		BufferedReader errorBR = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		String temp;
		bw = new BufferedWriter(new FileWriter(getDesktop()+"info.txt"));
		while((temp=infoBR.readLine())!=null){
			bw.write(temp);
			bw.flush();
		}
		bw.close();
		bw = new BufferedWriter(new FileWriter(getDesktop()+"error.txt"));
		while((temp=errorBR.readLine())!=null){
			bw.write(temp);
			bw.flush();
		}
		bw.close();
	}
	private static void writeIntoFile(File f, String cmd) throws IOException {
		if(f.exists())f.delete();BufferedWriter bw;
		bw = new BufferedWriter(new FileWriter(f));
		bw.write(cmd);
		bw.close();		
	}
	private static String getCMD() {
		String url = "http://10.10.10.46:8080/sse-spm/sys/biz/workload/uajax/doSign.html?comments=&_="+(new Date().getTime() + "");
		url = "www.baidu.com";
		String cmd = "start "+ url;
		return cmd;
	}
	private static Process run(String string) throws IOException {
		return Runtime.getRuntime().exec(string);
	}
	private static void open(File file) throws IOException {
		java.awt.Desktop.getDesktop().open(file);
	}
	private static String getDesktop() {
		return System.getenv("USERPROFILE") + "\\desktop\\";
	}
}
